﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BoringTodo
{
    public partial class EditTaskForm : Form
    {
        public TodoItem Task { get; private set; }
        private Button btnCancel;

        public EditTaskForm(TodoItem task)
        {
            InitializeComponent();
            
            // 添加null检查
            if (task == null)
            {
                task = new TodoItem
                {
                    描述 = "",
                    备注 = "",
                    预期完成时间 = DateTime.Now,
                    任务类型 = TaskType.杂活
                };
            }
            
            Task = task;

            // 设置窗体的 AcceptButton 为 btnOK
            this.AcceptButton = btnOK;

            // 将任务的当前数据填充到表单字段中 - 确保显示正确数据
            txtDescription.Text = Task.描述 ?? string.Empty;

            // 检查预期完成时间是否有值
            if (Task.预期完成时间.HasValue)
            {
                dtpExpectedCompletion.Value = Task.预期完成时间.Value;
            }
            else
            {
                // 如果没有值，设置为当前时间
                dtpExpectedCompletion.Value = DateTime.Now;
            }

            // 确保备注也正确显示
            txtRemarks.Text = Task.备注 ?? string.Empty;

            // 设置任务类型单选按钮
            SetTaskTypeSelection(Task.任务类型);
            
            // 改进 EditTaskForm 的用户体验
            InitializeForm();
            
            // 使所有控件可见，确保按钮正确显示
            this.btnOK.Visible = true;
            this.btnOK.Enabled = true;
        }

        private void InitializeForm()
        {
            // 设置窗体标题
            this.Text = "编辑任务";
            
            // 添加取消按钮
            btnCancel = new Button();
            btnCancel.Text = "取消";
            btnCancel.Location = new System.Drawing.Point(166, 395);
            btnCancel.Size = new System.Drawing.Size(120, 45);
            btnCancel.Click += btnCancel_Click;
            btnCancel.UseVisualStyleBackColor = true; // 使用与其他按钮一致的样式
            this.Controls.Add(btnCancel);
            
            // 调整确认按钮大小和位置，并确保它在最上层显示
            btnOK.Text = "确认";
            btnOK.Size = new System.Drawing.Size(120, 45);
            btnOK.Location = new System.Drawing.Point(36, 395);
            btnOK.UseVisualStyleBackColor = true;
            btnOK.Visible = true;
            btnOK.BringToFront();
            
            // 设置 CancelButton 属性
            this.CancelButton = btnCancel;
            
            // 调整窗体大小以适应内容
            this.ClientSize = new System.Drawing.Size(550, 480);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            // 使用表单数据更新任务
            Task.描述 = txtDescription.Text;
            Task.预期完成时间 = dtpExpectedCompletion.Value;
            Task.备注 = txtRemarks.Text;
            
            // 保存任务类型 - 支持所有5种任务类型
            if (radioUP.Checked)
                Task.任务类型 = TaskType.UP类;
            else if (radioJump.Checked)
                Task.任务类型 = TaskType.jump类;
            else if (radioOther.Checked)
                Task.任务类型 = TaskType.其他类;
            else if (radioValueWork.Checked)
                Task.任务类型 = TaskType.有价值工作;
            else if (radioMisc.Checked)
                Task.任务类型 = TaskType.杂活;
            else
                Task.任务类型 = TaskType.其他类; // 默认值
            
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void SetTaskTypeSelection(TaskType taskType)
        {
            // 先取消所有单选按钮的选中状态
            radioUP.Checked = false;
            radioJump.Checked = false;
            radioOther.Checked = false;
            radioValueWork.Checked = false;
            radioMisc.Checked = false;

            switch (taskType)
            {
                case TaskType.UP类:
                    radioUP.Checked = true;
                    break;
                case TaskType.jump类:
                    radioJump.Checked = true;
                    break;
                case TaskType.其他类:
                    radioOther.Checked = true;
                    break;
                case TaskType.有价值工作:
                    radioValueWork.Checked = true;
                    break;
                case TaskType.杂活:
                    radioMisc.Checked = true;
                    break;
                default:
                    // 默认选择"其他类"
                    radioOther.Checked = true;
                    break;
            }
            
            // 确保至少有一个单选按钮被选中
            if (!radioUP.Checked && !radioJump.Checked && !radioOther.Checked && !radioValueWork.Checked && !radioMisc.Checked)
            {
                radioOther.Checked = true;
            }
        }
    }
}